home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / DESKLIBC / SOURCES.ZIP / DeskLib / !DLSources / Libraries / Clear / c / Load < prev    next >
Text File  |  1995-07-08  |  3KB  |  119 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for 
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #                                      
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    Clear.c.Load
  12.     Author:  Copyright © 1993,1994 Jason Howat
  13.     Version: 1.01 (13 May 1994)
  14.     Purpose: Load a Clear file from disk.
  15.     History: 1.00 (16 Dec 1993)   initial version
  16.              1.01 (13 May 1994)   updated to use Mem_ library
  17. */
  18.  
  19. #include <stdlib.h>
  20.  
  21. #include "DeskLib:Clear.h"
  22. #include "DeskLib:File.h"
  23. #include "DeskLib:Mem.h"
  24.  
  25.  
  26. static clear_picture *Clear__AbortLoad(file_handle in, clear_picture *temp)
  27. {
  28.   if(in)
  29.     File_Close(in);
  30.  
  31.   if(temp)
  32.   {
  33.     if(temp->bitmap)
  34.       Mem_Free((void **)&temp->bitmap);
  35.     if(temp->palette)
  36.       Mem_Free((void **)&temp->palette);
  37.     free(temp);
  38.   }
  39.  
  40.   return NULL;
  41. }
  42.  
  43. clear_picture *Clear_Load(char *filename)
  44. {
  45.   file_handle   in = 0;
  46.   clear_picture *temp = NULL;
  47.   unsigned      creator_length = 1;
  48.   unsigned      bitmap_size;
  49.   int           word;
  50.   unsigned      entry,
  51.                 colours;
  52.   unsigned      i;
  53.   palette_entry *palette;
  54.  
  55.  
  56.   if((in = File_Open(filename, file_READ)) == 0)
  57.     return NULL;
  58.  
  59.   while(File_ReadByte(in) > 0)
  60.     creator_length++;
  61.  
  62.   if(file_lasterror)
  63.     return Clear__AbortLoad(in, temp);
  64.  
  65.   if(File_Seek(in, 0))
  66.     return Clear__AbortLoad(in, temp);
  67.  
  68.   if((temp = malloc(sizeof(clear_picture) + creator_length)) == NULL)
  69.     return Clear__AbortLoad(in, temp);
  70.  
  71.   temp->creator = (char *)&temp[1];
  72.   temp->palette = NULL;
  73.   temp->bitmap = NULL;
  74.  
  75.   if(File_ReadBytes(in, temp->creator, creator_length))
  76.     return Clear__AbortLoad(in, temp);
  77.  
  78.   temp->creatorversion = File_Read32(in);
  79.   temp->width = File_Read32(in);
  80.   temp->height = File_Read32(in);
  81.   temp->bpp = File_Read32(in);
  82.  
  83.   if(file_lasterror)
  84.     return Clear__AbortLoad(in, temp);
  85.  
  86.   if(temp->bpp > 8)
  87.     bitmap_size = 3 * temp->width * temp->height;
  88.   else
  89.   {
  90.     colours = 1 << temp->bpp;
  91.  
  92.     if(!Mem_Alloc((void **)&temp->palette, sizeof(palette_entry) * colours))
  93.       return Clear__AbortLoad(in, temp);
  94.  
  95.     palette = temp->palette;
  96.     for(entry = 0 ; entry < colours ; entry++)
  97.     {
  98.       word = 0;
  99.       for(i = 8 ; i < 25 ; i += 8)
  100.         word += File_ReadByte(in) << i;
  101.       palette[entry].value = word;
  102.     }
  103.  
  104.     if(file_lasterror)
  105.       return Clear__AbortLoad(in, temp);
  106.  
  107.     bitmap_size = temp->width * temp->height;
  108.   }
  109.  
  110.   if(!Mem_Alloc((void **)&temp->bitmap, bitmap_size))
  111.     return Clear__AbortLoad(in, temp);
  112.  
  113.   if(File_ReadBytes(in, temp->bitmap, bitmap_size) || file_lasterror)
  114.     return Clear__AbortLoad(in, temp);
  115.  
  116.   File_Close(in);
  117.   return temp;
  118. }
  119.